import assert from "node:assert";
import { test } from "node:test";
import { resolvedThroughFallback } from "@arethetypeswrong/core/utils";

test("resolvedThroughFallback", () => {
  assert.ok(!resolvedThroughFallback([]));

  assert.ok(
    resolvedThroughFallback([
      "======== Resolving module 'dep' from '/index.mts'. ========",
      "Explicitly specified module resolution kind: 'Bundler'.",
      "File '/package.json' does not exist.",
      "Loading module 'dep' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
      "Found 'package.json' at '/node_modules/dep/package.json'.",
      "Entering conditional exports.",
      "Matched 'exports' condition 'import'.",
      "Using 'exports' subpath '.' with target './dist/index.mjs'.",
      "File name '/node_modules/dep/dist/index.mjs' has a '.mjs' extension - stripping it.",
      "File '/node_modules/dep/dist/index.mts' does not exist.",
      "File '/node_modules/dep/dist/index.d.mts' does not exist.",
      "Failed to resolve under condition 'import'.",
      "Saw non-matching condition 'require'.",
      "Matched 'exports' condition 'types'.",
      "Using 'exports' subpath '.' with target './dist/index.d.ts'.",
      "File '/node_modules/dep/dist/index.d.ts' exists - use it as a name resolution result.",
      "Resolved under condition 'types'.",
      "Exiting conditional exports.",
      "Resolving real path for '/node_modules/dep/dist/index.d.ts', result '/node_modules/dep/dist/index.d.ts'.",
      "======== Module name 'dep' was successfully resolved to '/node_modules/dep/dist/index.d.ts' with Package ID 'dep/dist/index.d.ts@1.0.0'. ========",
    ]),
  );

  assert.ok(
    !resolvedThroughFallback([
      "======== Resolving module 'dual' from '/main.ts'. ========",
      "Explicitly specified module resolution kind: 'Bundler'.",
      "File '/package.json' does not exist.",
      "Loading module 'dual' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
      "Found 'package.json' at '/node_modules/dual/package.json'.",
      "Entering conditional exports.",
      "Matched 'exports' condition 'import'.",
      "Using 'exports' subpath '.' with target './index.js'.",
      "File name '/node_modules/dual/index.js' has a '.js' extension - stripping it.",
      "File '/node_modules/dual/index.ts' does not exist.",
      "File '/node_modules/dual/index.tsx' does not exist.",
      "File '/node_modules/dual/index.d.ts' exists - use it as a name resolution result.",
      "Resolved under condition 'import'.",
      "Exiting conditional exports.",
      "Resolving real path for '/node_modules/dual/index.d.ts', result '/node_modules/dual/index.d.ts'.",
      "======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
      "======== Resolving module 'dual' from '/main.mts'. ========",
      "Resolution for module 'dual' was found in cache from location '/'.",
      "======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
      "======== Resolving module 'dual' from '/main.cts'. ========",
      "Resolution for module 'dual' was found in cache from location '/'.",
      "======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
    ]),
  );

  assert.ok(
    !resolvedThroughFallback([
      "File '/node_modules/exports-and-types-versions/types/package.json' does not exist.",
      "Found 'package.json' at '/node_modules/exports-and-types-versions/package.json'.",
      "File '/node_modules/just-types-versions/types/package.json' does not exist.",
      "Found 'package.json' at '/node_modules/just-types-versions/package.json'.",
      "======== Resolving module 'exports-and-types-versions/foo' from '/main.cts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in CJS mode with conditions 'node', 'require', 'types'.",
      "File '/package.json' does not exist.",
      "Loading module 'exports-and-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Using 'exports' subpath './foo' with target './dist/foo.js'.",
      "File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.",
      "File '/node_modules/exports-and-types-versions/dist/foo.ts' does not exist.",
      "File '/node_modules/exports-and-types-versions/dist/foo.tsx' does not exist.",
      "File '/node_modules/exports-and-types-versions/dist/foo.d.ts' does not exist.",
      "Export specifier './foo' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Using 'exports' subpath './foo' with target './dist/foo.js'.",
      "File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.",
      "File '/node_modules/exports-and-types-versions/dist/foo.js' exists - use it as a name resolution result.",
      "Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.",
      "======== Module name 'exports-and-types-versions/foo' was successfully resolved to '/node_modules/exports-and-types-versions/dist/foo.js' with Package ID 'exports-and-types-versions/dist/foo.js@1.0.0'. ========",
      "======== Resolving module 'exports-and-types-versions/nope' from '/main.cts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in CJS mode with conditions 'node', 'require', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/nope' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Export specifier './nope' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Export specifier './nope' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "======== Module name 'exports-and-types-versions/nope' was not resolved. ========",
      "======== Resolving module 'exports-and-types-versions/yep' from '/main.cts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in CJS mode with conditions 'node', 'require', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/yep' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Matched 'exports' condition 'types'.",
      "Using 'exports' subpath './yep' with target './types/foo.d.ts'.",
      "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolved under condition 'types'.",
      "Exiting conditional exports.",
      "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.",
      "======== Module name 'exports-and-types-versions/yep' was successfully resolved to '/node_modules/exports-and-types-versions/types/foo.d.ts' with Package ID 'exports-and-types-versions/types/foo.d.ts@1.0.0'. ========",
      "======== Resolving module 'exports-and-types-versions/versioned-yep' from '/main.cts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in CJS mode with conditions 'node', 'require', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/versioned-yep' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Matched 'exports' condition 'types@>=4'.",
      "Using 'exports' subpath './versioned-yep' with target './types/foo.d.ts'.",
      "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolved under condition 'types@>=4'.",
      "Exiting conditional exports.",
      "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.",
      "======== Module name 'exports-and-types-versions/versioned-yep' was successfully resolved to '/node_modules/exports-and-types-versions/types/foo.d.ts' with Package ID 'exports-and-types-versions/types/foo.d.ts@1.0.0'. ========",
      "======== Resolving module 'exports-and-types-versions/versioned-nah' from '/main.cts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in CJS mode with conditions 'node', 'require', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/versioned-nah' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Saw non-matching condition 'types@<4'.",
      "Exiting conditional exports.",
      "Export specifier './versioned-nah' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Saw non-matching condition 'types@<4'.",
      "Exiting conditional exports.",
      "Export specifier './versioned-nah' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "======== Module name 'exports-and-types-versions/versioned-nah' was not resolved. ========",
      "======== Resolving module 'just-types-versions/foo' from '/main.cts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in CJS mode with conditions 'node', 'require', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'just-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/just-types-versions/package.json' exists according to earlier cached lookups.",
      "'package.json' has a 'typesVersions' field with version-specific path mappings.",
      "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'foo'.",
      "Module name 'foo', matched pattern 'foo'.",
      "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
      "File '/node_modules/just-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolving real path for '/node_modules/just-types-versions/types/foo.d.ts', result '/node_modules/just-types-versions/types/foo.d.ts'.",
      "======== Module name 'just-types-versions/foo' was successfully resolved to '/node_modules/just-types-versions/types/foo.d.ts'. ========",
      "======== Resolving module 'exports-and-types-versions/foo' from '/main.mts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in ESM mode with conditions 'node', 'import', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Using 'exports' subpath './foo' with target './dist/foo.js'.",
      "File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.",
      "File '/node_modules/exports-and-types-versions/dist/foo.ts' does not exist.",
      "File '/node_modules/exports-and-types-versions/dist/foo.tsx' does not exist.",
      "File '/node_modules/exports-and-types-versions/dist/foo.d.ts' does not exist.",
      "Export specifier './foo' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Using 'exports' subpath './foo' with target './dist/foo.js'.",
      "File name '/node_modules/exports-and-types-versions/dist/foo.js' has a '.js' extension - stripping it.",
      "File '/node_modules/exports-and-types-versions/dist/foo.js' exists - use it as a name resolution result.",
      "Resolution of non-relative name failed; trying with modern Node resolution features disabled to see if npm library needs configuration update.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "'package.json' has a 'typesVersions' field with version-specific path mappings.",
      "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'foo'.",
      "Module name 'foo', matched pattern 'foo'.",
      "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
      "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolving real path for '/node_modules/exports-and-types-versions/dist/foo.js', result '/node_modules/exports-and-types-versions/dist/foo.js'.",
      "======== Module name 'exports-and-types-versions/foo' was successfully resolved to '/node_modules/exports-and-types-versions/dist/foo.js' with Package ID 'exports-and-types-versions/dist/foo.js@1.0.0'. ========",
      "======== Resolving module 'exports-and-types-versions/nope' from '/main.mts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in ESM mode with conditions 'node', 'import', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/nope' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Export specifier './nope' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Export specifier './nope' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "======== Module name 'exports-and-types-versions/nope' was not resolved. ========",
      "======== Resolving module 'exports-and-types-versions/yep' from '/main.mts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in ESM mode with conditions 'node', 'import', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/yep' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Matched 'exports' condition 'types'.",
      "Using 'exports' subpath './yep' with target './types/foo.d.ts'.",
      "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolved under condition 'types'.",
      "Exiting conditional exports.",
      "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.",
      "======== Module name 'exports-and-types-versions/yep' was successfully resolved to '/node_modules/exports-and-types-versions/types/foo.d.ts' with Package ID 'exports-and-types-versions/types/foo.d.ts@1.0.0'. ========",
      "======== Resolving module 'exports-and-types-versions/versioned-yep' from '/main.mts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in ESM mode with conditions 'node', 'import', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/versioned-yep' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Matched 'exports' condition 'types@>=4'.",
      "Using 'exports' subpath './versioned-yep' with target './types/foo.d.ts'.",
      "File '/node_modules/exports-and-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolved under condition 'types@>=4'.",
      "Exiting conditional exports.",
      "Resolving real path for '/node_modules/exports-and-types-versions/types/foo.d.ts', result '/node_modules/exports-and-types-versions/types/foo.d.ts'.",
      "======== Module name 'exports-and-types-versions/versioned-yep' was successfully resolved to '/node_modules/exports-and-types-versions/types/foo.d.ts' with Package ID 'exports-and-types-versions/types/foo.d.ts@1.0.0'. ========",
      "======== Resolving module 'exports-and-types-versions/versioned-nah' from '/main.mts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in ESM mode with conditions 'node', 'import', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'exports-and-types-versions/versioned-nah' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Saw non-matching condition 'types@<4'.",
      "Exiting conditional exports.",
      "Export specifier './versioned-nah' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
      "File '/node_modules/exports-and-types-versions/package.json' exists according to earlier cached lookups.",
      "Entering conditional exports.",
      "Saw non-matching condition 'types@<4'.",
      "Exiting conditional exports.",
      "Export specifier './versioned-nah' does not exist in package.json scope at path '/node_modules/exports-and-types-versions'.",
      "======== Module name 'exports-and-types-versions/versioned-nah' was not resolved. ========",
      "======== Resolving module 'just-types-versions/foo' from '/main.mts'. ========",
      "Module resolution kind is not specified, using 'Node16'.",
      "Resolving in ESM mode with conditions 'node', 'import', 'types'.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "Loading module 'just-types-versions/foo' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration.",
      "File '/node_modules/just-types-versions/package.json' exists according to earlier cached lookups.",
      "'package.json' has a 'typesVersions' entry '*' that matches compiler version '3.1.0-dev', looking for a pattern to match module name 'foo'.",
      "Module name 'foo', matched pattern 'foo'.",
      "Trying substitution './types/foo.d.ts', candidate module location: './types/foo.d.ts'.",
      "File '/node_modules/just-types-versions/types/foo.d.ts' exists - use it as a name resolution result.",
      "Resolving real path for '/node_modules/just-types-versions/types/foo.d.ts', result '/node_modules/just-types-versions/types/foo.d.ts'.",
      "======== Module name 'just-types-versions/foo' was successfully resolved to '/node_modules/just-types-versions/types/foo.d.ts'. ========",
      "File 'package.json' does not exist.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "File 'package.json' does not exist according to earlier cached lookups.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "File 'package.json' does not exist according to earlier cached lookups.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "File 'package.json' does not exist according to earlier cached lookups.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "File 'package.json' does not exist according to earlier cached lookups.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "File 'package.json' does not exist according to earlier cached lookups.",
      "File '/package.json' does not exist according to earlier cached lookups.",
      "File 'package.json' does not exist according to earlier cached lookups.",
      "File '/package.json' does not exist according to earlier cached lookups.",
    ]),
  );
});
